package project;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import com.toedter.calendar.*;

public class MainGUI extends JPanel implements ActionListener
{
	private JFrame frame;
	private JPanel topPan, bottomPan, searchResultsPanel;
	private JTextArea bottomJTA;
	private JButton rentB, returnB, bookB;
	
	//top panel components
	private JTextField searchJTF;
	private JComboBox searchJCB;
	private JButton searchB, setB, addB, otherB;
	private JTextArea topJTA;
	
	//Main Panel Changer
	private JPanel changePanel;
	
	//Boxes
	private Box yBox1, yBox2;
	
	//Date Choosers
	private JDateChooser fromDC, toDC;
	
	
	
	
	public MainGUI() 
	{
		//Text Areas
		
		bottomJTA = new JTextArea();
		bottomJTA.setPreferredSize(new Dimension(400,250));
		bottomJTA.setText("THE BOTTOM!");
		bottomJTA.setBorder(BorderFactory.createEtchedBorder());
		
		//Filler Dimensons
		Dimension xd = new Dimension(5,1);
		Dimension yd = new Dimension(1,5);
		Dimension d1 = new Dimension(1,20);
		Dimension d2 = new Dimension(1,100);
		Dimension d3 = new Dimension(1,Short.MAX_VALUE);
		
		//images
		ImageIcon rightArrow = new ImageIcon("C:/Users/James/Pictures/LoanTurtle/CircleArrow.png");
		
		//Boxes
		searchJTF = new JTextField(30);
		searchJTF.setMaximumSize(new Dimension(200,20));
		String [] jcbString = new String[]{"Barcode", "Title", "Year"};
		searchJCB = new JComboBox(jcbString);
		searchB = new JButton("Search!");
		searchB.addActionListener(this);
		setB = new JButton("Set Satus", rightArrow);
		setB.setHorizontalAlignment(SwingConstants.LEFT);
		removeButtonStuff(setB);
		addB = new JButton("Add", rightArrow);
		addB.setHorizontalAlignment(SwingConstants.LEFT);
		removeButtonStuff(addB);
		addB.setPreferredSize(setB.getPreferredSize());
		addB.setMaximumSize(setB.getMaximumSize());
		otherB = new JButton("Other", rightArrow);
		otherB.setHorizontalAlignment(SwingConstants.LEFT);
		removeButtonStuff(otherB);
		otherB.setMaximumSize(setB.getMaximumSize());
		topJTA = new JTextArea();
		topJTA.setPreferredSize(new Dimension(200,200));
		topJTA.setText("THE TOP!");
		topJTA.setBorder(BorderFactory.createEtchedBorder());
		Box xBox2 = new Box(BoxLayout.X_AXIS);
		xBox2.add(new Box.Filler(xd,xd,xd));
		xBox2.add(searchJTF);
		xBox2.add(new Box.Filler(xd,xd,xd));
		xBox2.add(searchJCB);
		xBox2.add(new Box.Filler(xd,xd,xd));
		xBox2.add(searchB);
		xBox2.add(new Box.Filler(xd,xd,xd));
		
		
		
		yBox1 = new Box(BoxLayout.Y_AXIS);
		yBox1.add(setB);
		yBox1.add(addB);
		yBox1.add(otherB);
		
		
		
		Box xBox3 = new Box(BoxLayout.X_AXIS);
		xBox3.add(new Box.Filler(xd,xd,xd));
		searchResultsPanel = new JPanel(new GridLayout(1,1));
		searchResultsPanel.add(topJTA);
		xBox3.add(searchResultsPanel);
		xBox3.add(new Box.Filler(xd,xd,xd));
		xBox3.add(yBox1);
		xBox3.add(new Box.Filler(xd,new Dimension(100,1),xd));
		
		yBox2 = new Box(BoxLayout.Y_AXIS);
		JLabel datesL = new JLabel("Dates:", JLabel.CENTER);
		JPanel datesP = new JPanel(new GridLayout(1,1));
		datesP.add(datesL);
		yBox2.add(datesP);
		JLabel fromL = new JLabel("From:");
		yBox2.add(fromL);
		//fromBox.add(fromL);
		JPanel fromP = new JPanel();
		fromDC = new JDateChooser();
		fromDC.setPreferredSize(new Dimension(100,30));
		fromP.add(fromDC);
		yBox2.add(fromP);
		yBox2.add(Box.createVerticalStrut(5));
		JLabel toL = new JLabel("To:");
		yBox2.add(toL);
		JPanel toP = new JPanel();
		toDC = new JDateChooser();
		toDC.setPreferredSize(new Dimension(100,30));
		toP.add(toDC);
		yBox2.add(toP);
		yBox2.add(Box.createVerticalStrut(5));
		//yBox2.add(new Box.Filler(d1,d2,d3));
		
		
		Box yBox3 = new Box(BoxLayout.Y_AXIS);
		yBox3.add(xBox2);
		yBox3.add(Box.createVerticalStrut(5));
		yBox3.add(toL);
		yBox3.add(xBox3);
		
		Box xBox4 = new Box(BoxLayout.X_AXIS);
		xBox4.add(Box.createHorizontalStrut(5));
		xBox4.add(yBox2);
		xBox4.add(yBox3);
		
		
		
		
		
		//ButtonPanel
		rentB = new JButton("RENT");
		rentB.setMaximumSize(new Dimension(Short.MAX_VALUE,40));
		//rentB.setBackground(new Color(245,124,45));
		rentB.setBorder(BorderFactory.createEtchedBorder());
		removeButtonStuff(rentB);
		rentB.setBorderPainted(true);
		rentB.addActionListener(this);
		returnB = new JButton("RETURN");
		//returnB.setBackground(new Color(25,200,154));
		returnB.setBorder(BorderFactory.createEtchedBorder());
		removeButtonStuff(returnB);
		returnB.setBorderPainted(true);
		returnB.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
		returnB.addActionListener(this);
		bookB = new JButton("BOOK");
		//bookB.setBackground(new Color(125,100,195));
		bookB.setBorder(BorderFactory.createEtchedBorder());
		removeButtonStuff(bookB);
		bookB.setBorderPainted(true);
		bookB.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
		bookB.addActionListener(this);
		Box xBox1 = new Box(BoxLayout.LINE_AXIS);
		xBox1.add(rentB);
		xBox1.add(returnB);
		xBox1.add(bookB);
		xBox1.setPreferredSize(new Dimension(Short.MAX_VALUE, 30));
		
		//Add Filler
		//Dimension minSize = new Dimension(50, 20);
		//Dimension prefSize = new Dimension(100, 20);
		//Dimension maxSize = new Dimension(Short.MAX_VALUE, 20);
		//xBox1.add(new Box.Filler(minSize, prefSize, maxSize));
		
		//this JPanel
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.add(xBox1);
		this.add(new Box.Filler(yd,yd,yd));
		this.add(xBox4);
		this.add(new Box.Filler(yd,yd,yd));
		changePanel = new JPanel(new GridLayout(1,1));
		changePanel.add(new RentGUI());
		this.add(changePanel);
		this.setBorder(BorderFactory.createMatteBorder(6,6,6,6,rentB.getBackground()));
		//Add Filler
		//Dimension minSize = new Dimension(50, 20);
		//Dimension prefSize = new Dimension(100, 300);
		//Dimension maxSize = new Dimension(20, 100);
		//this.add(new Box.Filler(minSize, prefSize, maxSize));
		
		//frame
		frame = new JFrame("Just a little test...");
		frame.setSize(800,600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(this, BorderLayout.CENTER);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		
		

	}
	public void removeButtonStuff(JButton button)
	{
		button.setBackground(new Color(238,238,238));
		button.setBorderPainted(false);
		button.setFocusPainted(false);
	}
	
	public void resetRentButtonBorder()
	{
		if(rentB.getBorder() == null)
		{
			//rentB.setBorder(BorderFactory.createRaisedBevelBorder());
		}
	}
	public void resetReturnButtonBorder()
	{
		if(returnB.getBorder() == null)
		{
			//returnB.setBorder(BorderFactory.createRaisedBevelBorder());
		}
	}
	public void resetBookButtonBorder()
	{
		if(bookB.getBorder() == null)
		{
			//bookB.setBorder(BorderFactory.createRaisedBevelBorder());
		}
	}
	
	//***************
	//actionPerformed
	//***************
	public void actionPerformed(ActionEvent ae)
	{
		System.out.println(changePanel.getComponentCount());
		Object source = ae.getSource();
		if(source == searchB)
		{
			searchResultsPanel.removeAll();
			Table table = new Table();
			JScrollPane jsp = new JScrollPane(table.createStockSearchTable(searchJTF.getText()));
			jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			searchResultsPanel.add(jsp);
			searchResultsPanel.validate();
		}
		if(source == rentB)
		{
			changePanel.removeAll();
			changePanel.add(new RentGUI());
			changePanel.validate();
			this.setBorder(BorderFactory.createMatteBorder(6,6,6,6,rentB.getBackground()));
			//rentB.setBorder(null);
			resetReturnButtonBorder();
			resetBookButtonBorder();
		}
		if(source == returnB)
		{
			changePanel.removeAll();
			changePanel.add(new ReturnGUI());
			changePanel.validate();
			this.setBorder(BorderFactory.createMatteBorder(6,6,6,6,returnB.getBackground()));
			//returnB.setBorder(null);
			resetRentButtonBorder();
			resetBookButtonBorder();
		}
		if(source == bookB)
		{
			changePanel.removeAll();
			changePanel.add(new BookGUI());
			changePanel.validate();
			this.setBorder(BorderFactory.createMatteBorder(6,6,6,6,bookB.getBackground()));
			//bookB.setBorder(null);
			resetRentButtonBorder();
			resetReturnButtonBorder();
			
			System.out.println("frame content pane color: " + frame.getContentPane().getBackground());
		}
	}
	
	

	
	public static void main(String[] args) 
	{
		new MainGUI();
	}	
}
